//
//  MBStatisticsViewController.m
//  MemberBusiness-ios
//
//  Created by cherry pu on 2018/4/19.
//  Copyright © 2018年 zhangXin. All rights reserved.
//

#import "MBStatisticsViewController.h"
#import "MBGoodsTypeView.h"
#import "MBStatisticsExcelView.h"
#import "MBSaleSectionView.h"
#import "MBStatisticsTableViewCell.h"
#import "MBStatisticsMonthPickerView.h"

@interface MBStatisticsViewController ()
@property (nonatomic,weak)UIScrollView* scrollView;

@property (nonatomic,strong) MBStatisticsMonthPickerView* pickView;
@property (nonatomic,weak)MBStatisticsExcelView* excelView1;
@property (nonatomic,weak)MBStatisticsExcelView* excelView2;
@property (nonatomic,weak)MBGoodsTypeView* typeView;

@end

@implementation MBStatisticsViewController
@dynamic viewModel;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupLayout];
    
    [self setUpData];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
//    NSDate *now = [NSDate date];
//    NSCalendar *cal = [NSCalendar currentCalendar];
//    unsigned int unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
//    NSDateComponents *dd = [cal components:unitFlags fromDate:now];
//    NSInteger year = [dd year];
//    NSInteger month = [dd month];
//    if (year != self.viewModel.year || month != self.viewModel.monthy) {
//        self.viewModel.year = year;
//        self.viewModel.monthy = month;
//        [self.excelView1.dayButton setTitle:[NSString stringWithFormat:@"%ld年%ld月",(long)year,(long)month] forState:UIControlStateNormal];
//        [self setUpDataWithType:0];
//    }
//    
//    if (year != self.viewModel.year2 || month != self.viewModel.monthy2) {
//        self.viewModel.year2 = year;
//        self.viewModel.monthy2 = month;
//        [self.excelView2.dayButton setTitle:[NSString stringWithFormat:@"%ld年%ld月",(long)year,(long)month] forState:UIControlStateNormal];
//        [self setUpDataWithType:1];
//    }
}

-(void)setupLayout
{
    [super setupLayout];
    
    self.naviView.titleLabel.text = @"统计结算";
    
    MBGoodsTypeView* typeView = [[MBGoodsTypeView alloc]initWithTypeArray:@[@"店铺概况",@"运营报告",@"销售排行"]];
    self.typeView = typeView;
    [self.view addSubview:typeView];
    
    [typeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(SCREEN_STATUS_BAR_HEIGHT+44);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo([UIView lf_sizeFromIphone6:44]);
    }];
    
    UIScrollView* scrollView = [[UIScrollView alloc]init];
    self.scrollView = scrollView;
    scrollView.scrollEnabled = NO;
    scrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:scrollView];
    
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(typeView.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
    }];

    MBStatisticsExcelView* excelView1 = [[MBStatisticsExcelView alloc]init];
    self.excelView1 = excelView1;
    [scrollView addSubview:excelView1];
    
    [excelView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(scrollView);
        make.width.mas_offset(SCREEN_WIDTH);
        make.height.mas_equalTo(SCREEN_HEIGHT-44-SCREEN_STATUS_BAR_HEIGHT-[UIView lf_sizeFromIphone6:44]-self.tabBarController.tabBar.height);
    }];
    
    MBStatisticsExcelView* excelView2 = [[MBStatisticsExcelView alloc]init];
    self.excelView2 = excelView2;
    [scrollView addSubview:excelView2];
    
    [excelView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(scrollView);
        make.width.mas_offset(SCREEN_WIDTH);
        make.left.equalTo(scrollView).offset(SCREEN_WIDTH);
        make.height.mas_equalTo(SCREEN_HEIGHT-44-SCREEN_STATUS_BAR_HEIGHT-[UIView lf_sizeFromIphone6:44]-self.tabBarController.tabBar.height);
    }];
    
    [self.tableView removeFromSuperview];
    [self setUpMjRefresh];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    [scrollView addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.equalTo(scrollView);
        make.width.mas_offset(SCREEN_WIDTH);
        make.left.equalTo(scrollView).offset(SCREEN_WIDTH*2);
        make.height.mas_equalTo(SCREEN_HEIGHT-44-SCREEN_STATUS_BAR_HEIGHT-[UIView lf_sizeFromIphone6:44]-self.tabBarController.tabBar.height);        
    }];
    
    MBSaleSectionView* saleSectionView = [[MBSaleSectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, [UIView lf_sizeFromIphone6:36])];
    self.tableView.tableHeaderView = saleSectionView;
    
    MBStatisticsMonthPickerView* pickView = [[MBStatisticsMonthPickerView alloc]init];
    self.pickView = pickView;
    
}

-(void)dealloc
{
    [self.pickView removeFromSuperview];
}

-(void)setUpData
{
    //    切换type
    @weakify(self);
    [RACObserve(self.typeView, type) subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        self.scrollView.contentOffset = CGPointMake(SCREEN_WIDTH*[x intValue], 0);
        if ([x intValue] != 2) {
            [self setUpDataWithType:[x intValue]];
        }else {
            [self pullTableViewRequestData:YES];
        }
    }];
    
    __block int excelView = 1;
    //    切换日期
    [[self.excelView1.dayButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        excelView = 1;
        self.pickView.year = self.viewModel.year;
        self.pickView.monthy = self.viewModel.monthy;
        [self.pickView show];
    }];
    
    //    切换日期
    [[self.excelView2.dayButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        excelView = 2;
        self.pickView.year = self.viewModel.year2;
        self.pickView.monthy = self.viewModel.monthy2;
        [self.pickView show];
    }];
    
    //    刷新
    [RACObserve(self.excelView1, isRefresh)subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        if ([x boolValue]) {
            [self setUpDataWithType:0];
        }
    }];
    
    //    刷新
    [RACObserve(self.excelView2, isRefresh)subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        if ([x boolValue]) {
            [self setUpDataWithType:1];
        }
    }];

    //    pickerView 确认
    [[self.pickView.shureButton rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [self.pickView close];
        if (excelView == 1) {
            self.viewModel.year = self.pickView.year;
            self.viewModel.monthy = self.pickView.monthy;
            [self.excelView1.dayButton setTitle:[NSString stringWithFormat:@"%ld年%ld月",(long)self.pickView.year,(long)self.pickView.monthy] forState:UIControlStateNormal];
            [self setUpDataWithType:0];
        }else {
            self.viewModel.year2 = self.pickView.year;
            self.viewModel.monthy2 = self.pickView.monthy;

            [self.excelView2.dayButton setTitle:[NSString stringWithFormat:@"%ld年%ld月",(long)self.pickView.year,(long)self.pickView.monthy] forState:UIControlStateNormal];
            [self setUpDataWithType:1];
        }
    }];
    
    
    [RACObserve(self.viewModel, profileModel) subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        self.excelView1.model = x;
    }];
    
    [RACObserve(self.viewModel,reportModel) subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        self.excelView2.reportModel = x;
    }];

    [RACObserve(self.viewModel, endRefresh) subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        if ([x intValue] != 0) {
            if (self.viewModel.endRefresh == 1) {
                self.excelView1.endRefresh = YES;
            }else {
                self.excelView2.endRefresh = YES;
            }
        }
    }];
}

-(void)setUpDataWithType:(int)type
{
    [self.viewModel sendRequestWithType:type result:^(bool success) {
        [self hideLoadingViewFooter];
        if (success) {
            [self.tableView reloadData];
        }
    }];
}

-(void)pullTableViewRequestData:(BOOL)isRefresh
{
    if (isRefresh) {
        [self.viewModel initRequestPullPage];
    }
    
    [self setUpDataWithType:2];
}

-(Class)cellClassForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [MBStatisticsTableViewCell class];
}

@end
